﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRMS_Model;
using DataManager;
using System.Diagnostics;
using System.Web.Mvc;
using HRMS.Models.ModelBinders;


namespace HRMS.Controllers
{
    public class SubskillsModels
    {
        public static bool AddSubSkill(SubSkill subskill)
        {
            try
            {
                using (var subEntity = new HRMSEntities())
                {
                    subEntity.SubSkills.AddObject(subskill);
                    subEntity.SaveChanges();
                    return true;
                }
            }

            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }

        }
        public static List<SubSkill> GetSubSkills(string sortExpression, string sortDirection, int pageIndex, int pageSize, int pageCount)
        {
            using (var subEntity = new HRMSEntities())
            {
                return subEntity.SubSkills.OrderBy("it." + sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageCount * pageSize).ToList();
            }
        }

        public static int GetSubSkillsCount()
        {
            using (var subEntity = new HRMSEntities())
            {
                return subEntity.SubSkills.Count();
            }
        }

        public static bool UpdateSubSkill([ModelBinder(typeof(CustomModelBinder))]SubSkill subskill)
        {
            try
            {
                using (var subEntity = new HRMSEntities())
                {
                    var subToEdit = subEntity.SubSkills.FirstOrDefault(r => r.SubSkillId == subskill.SubSkillId);
                    if (subToEdit != null)
                    {
                        subToEdit.SubSkillName= subskill.SubSkillName;
                        subToEdit.Information = subskill.Information;
                        ;
                        //empToEdit.Department.DepartmentName
                    }
                    subEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static bool DeleteSubSkill(int id)
        {
            try
            {
                using (var subskillEntity = new HRMSEntities())
                {
                    subskillEntity.SubSkills.DeleteObject(subskillEntity.SubSkills.First(e => e.SubSkillId == id));
                    subskillEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

    }
}